package modes

import (
	"fmt"
	"time"
)

/*
 * 描述: 科目表
 *
 * start 状态  0 启用  1 禁用
 *
 *****************************/

type Resource struct {
	Id      int64  `json:"r_id" xorm:"id"`           //资源ID
	PId     int64  `json:"r_pid" xorm:"pid"`         //项目ID
	SId     int64  `json:"r_sid" xorm:"sid"`         //科目ID
	Price   int64  `json:"r_price" xorm:"price"`     //资源价格
	RFormat int    `json:"r_format" xorm:"s_format"` //资源类型
	RType   int    `json:"r_type" xorm:"s_type"`     //资源类型
	At      int64  `json:"r_at" xorm:"at"`           //时间
	Url     string `json:"r_url" xorm:"url"`         //资源地址
	Icon    string `json:"r_icon" xorm:"icon"`       //展示地址
	Start   int    `json:"r_start" xorm:"start"`     //状态
	Nname   string `json:"r_nname" xorm:"nname"`     //资源名称
	Ssort   int64  `json:"r_s_sort" xorm:"s_sort"`   //排序字段（目前针对讲义上传问题）
}
type JoinResource struct {
	Resource `xorm:"extends"`
	Subject  `xorm:"extends"`
	Project  `xorm:"extends"`
}

func (this *Resource) TableName() string {
	return "resources"
}

func (this *Resource) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *Resource) Get() (bool, error) {
	var val Resource
	where := fmt.Sprintf("sid >= %d and sid < %d and url = '%s' and s_type = 0 and start = 0 ", this.SId, this.SId+1000, this.Url)
	return Db(0).Where(where).Get(&val)
}

func (this *Resource) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *Resource) GetSid() (int64, error) {
	var obj Resource
	var w int64
	if _, err := Db(0).Cols("id").
		Where("pid = ? ", this.PId).
		OrderBy("id desc").
		Get(&obj); err != nil {
		return 0, err
	}
	if obj.Id <= 0 {
		obj.Id = 100000000
	} else {
		_, _, w = RIdWhere(obj.Id)
	}

	//if w == 1000{
	//	this.Id = obj.Id + w / 1000
	//}else {
	//	this.Id = obj.Id + w / 100
	//}
	this.Id = obj.Id + w
	return this.Save()
}

func (this *Resource) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *Resource) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *JoinResource) PList(val *Pager) error {
	list := make([]JoinResource, 0)
	val.TName = "resources"
	val.Page--
	if val.Page <= 0 {
		val.Page = 0
	}
	var field, sort string
	switch val.Coll {
	case 1:
		field = "subject.name" // 名称
	case 2:
		field = fmt.Sprintf("%s.at", val.TName) // 时间
	case 3:
		field = fmt.Sprintf("%s.id", val.TName) // id
	default:
		field = fmt.Sprintf("%s.s_sort", val.TName) // id
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s desc", field)
	} else {
		sort = fmt.Sprintf("%s asc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).
		Join("INNER", "subject", fmt.Sprintf("%s.sid = subject.id", val.TName)).
		Join("INNER", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Where(val.ToWhere()).
		Count(this)
	err := Db(0).Table(val.TName).
		Join("INNER", "subject", fmt.Sprintf("%s.sid = subject.id", val.TName)).
		Join("INNER", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}
func (this *Resource) Count(where string) (int64, error) {
	var val Resource
	return Db(0).Where(where).Count(&val)
}

func (this *Resource) where(fage, count, page int, where, field string) ([]Resource, error) {
	list := make([]Resource, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
